package com.a0soft.gphone.aTruffleHog.wnd.info;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Patrick Cheng <patrick.taiwan (at) gmail.com>
 *
 */

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfo;
import com.a0soft.gphone.aTruffleHog.di.DeviceInfoKey;
import com.a0soft.gphone.aTruffleHog.util.CONSTS;

public class InfoMIView {
    private static final String MI_KEYS[] = {
            DeviceInfoKey.K.MI_RAM_TOTAL,
            DeviceInfoKey.K.MI_SWAP_TOTAL,
            DeviceInfoKey.K.MI_NATIVE_HEAP,
            DeviceInfoKey.K.MI_VM_MAX_MEM,
            DeviceInfoKey.K.MI_THRESHOLD
    };
    
    private Context _cxt = null;
    private DeviceInfo _di = null;
    
    public
    InfoMIView(Context cxt, DeviceInfo di)
    {
    	_cxt = cxt;
    	_di = di;
    }
    
    public View
    getView(boolean for_preview)
    {
        LinearLayout sv = new LinearLayout(_cxt);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        sv.setLayoutParams(params);
        sv.setOrientation(LinearLayout.VERTICAL);

        String total_src = _cxt.getResources().getString(R.string.di_sg_total) + " ";
        String avail_src = _cxt.getResources().getString(R.string.di_sg_avail) + " ";

        int childno = 0;
        for (int i=0; i<MI_KEYS.length; i++) {
            String info = _di.GetString(MI_KEYS[i]);
            if (!TextUtils.isEmpty(info) && !info.equals(CONSTS.UNAVAILABLE)) {
                View row = LayoutInflater.from(_cxt).inflate(R.layout.info_item,null);
                InfoItemViewWrapper wrapper = new InfoItemViewWrapper(row);
                row.setTag(wrapper);
                wrapper.setItemName(DeviceInfoKey.GetName(MI_KEYS[i]));
    			if (TextUtils.equals(MI_KEYS[i], DeviceInfoKey.K.MI_RAM_TOTAL)) {
        			long m_size = Long.parseLong(_di.GetString(MI_KEYS[i]));
        			long gb_v = m_size / InfoViewCONSTS.GB_LV; 
        	        if (gb_v > 0) {
        				double mi_v = ((double)m_size)/((double)InfoViewCONSTS.GB_LV);
        				info = total_src+new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_gb);
        			}
        			else {
        				double mi_v = ((double)m_size)/((double)InfoViewCONSTS.MB_LV);
        				info = total_src+new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_mb);
        			}

        	        if (for_preview == false) {
            	        // to calc the free mem
        				//final String[] strFields = new String[] {"MemFree:"};
       					//final long[] lSizes = new long[strFields.length];
       					//MiscUtil.ReadProcLines("/proc/meminfo", strFields, lSizes);
       					MemoryInfo mi = new MemoryInfo();
       					ActivityManager activityManager = (ActivityManager) _cxt.getSystemService(Activity.ACTIVITY_SERVICE);
       					activityManager.getMemoryInfo(mi);
       					long free_m_szie = mi.availMem; // / 1048576L;
       					//long free_m_szie = lSizes[0] * 1024;
       	    			long fgb_v = free_m_szie / InfoViewCONSTS.GB_LV; 
       	    			if (fgb_v > 0) {
       	    				double mi_v = ((double)free_m_szie)/((double)InfoViewCONSTS.GB_LV);
       	    				info += "\n" + avail_src + new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_gb);
       	    			}
       	    			else {
       	    				double mi_v = ((double)free_m_szie)/((double)InfoViewCONSTS.MB_LV);
       	    				info += "\n" + avail_src + new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_mb);
       	    			}
        	        }
    			}
    			else {
        			long m_size = Long.parseLong(_di.GetString(MI_KEYS[i]));
        			long gb_v = m_size / InfoViewCONSTS.GB_LV; 
        	        if (gb_v > 0) {
        				double mi_v = ((double)m_size)/((double)InfoViewCONSTS.GB_LV);
        				info = new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_gb);
        			}
        			else {
        				double mi_v = ((double)m_size)/((double)InfoViewCONSTS.MB_LV);
        				info = new DecimalFormat("#.##").format(mi_v) + " " + _cxt.getResources().getString(R.string.di_mi_mb);
        			}    				
    			}
                wrapper.setItemDetail(info);
                sv.addView(row);
                childno++;
            }               
        }
        return sv;
    }
}
